package lc.q150_200;

import java.util.Arrays;

public class Q189 {

    public void rotate2(int[] nums, int k) {
        // 先翻转数组
        int n = nums.length;
        k = k % n;
        for (int i = 0; i < n / 2; i++) {
            int now = nums[i];
            nums[i] = nums[n - i - 1];
            nums[n - i - 1] = now;
        }
        // 再翻转前面部分
        for (int i = 0; i < k / 2; i++) {
            int now = nums[i];
            nums[i] = nums[k - i - 1];
            nums[k - i - 1] = now;
        }
        // 翻转后面部分
        for (int i = 0; i < (n - k) / 2; i++) {
            int now = nums[i + k];
            nums[i + k] = nums[n - i - 1];
            nums[n - i - 1] = now;
        }
    }

    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k = k % n;
        int[] lastNums = Arrays.copyOfRange(nums, n - k, n);
        for (int j = n - 1; j >= k; j--) {
            nums[j] = nums[j - k];
        }
        for (int i = 0; i < k; i++) {
            nums[i] = lastNums[i];
        }
    }

    public static void main(String[] args) {
        Q189 q189 = new Q189();
        int[] nums = new int[]{1,2,3,4,5,6,7};
        q189.rotate2(nums, 3);
        System.out.println(Arrays.toString(nums));
    }
}
